package com.xinqi.modules.sys.platform.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.platform.convert.PlatformConvert;
import com.xinqi.modules.sys.platform.domain.PlatformEntity;
import com.xinqi.modules.sys.platform.dto.req.PlatformCreateDTO;
import com.xinqi.modules.sys.platform.dto.req.PlatformDeleteDTO;
import com.xinqi.modules.sys.platform.dto.req.PlatformQueryDTO;
import com.xinqi.modules.sys.platform.dto.req.PlatformUpdateDTO;
import com.xinqi.modules.sys.platform.dto.rsp.PlatformResultDTO;
import com.xinqi.modules.sys.platform.manager.PlatformManager;
import com.xinqi.modules.sys.platform.service.PlatformService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 系统平台服务层实现
 *
 * @author: linkai
 * @date: 2023/04/01
 */
@Validated
@Service("platformService")
@RequiredArgsConstructor
public class PlatformServiceImpl implements PlatformService {
    private final PlatformConvert platformConvert;
    private final PlatformManager platformManager;

    /**
     * 分页查询 系统平台数据
     */
    @Override
    @DS("slaver")
    public PageInfo<PlatformResultDTO> page(Param pageable, PlatformQueryDTO query) {
        IPage<PlatformEntity> page = platformManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, platformConvert::convert);
    }

    /**
     * 查询 系统平台数据
     */
    @Override
    public List<PlatformResultDTO> find(PlatformQueryDTO query) {
        return platformManager.list(wrapper(query)).stream()
            .map(platformConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<PlatformEntity> wrapper(PlatformQueryDTO query) {
        LambdaQueryWrapper<PlatformEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(PlatformEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 系统平台数据
     */
    @Override
    public PlatformResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(platformManager.findById(id)).map(platformConvert::convert).orElse(null);
    }

    /**
     * 新增 系统平台数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid PlatformCreateDTO dto) {
        PlatformEntity entity = platformConvert.create(dto);
        platformManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 系统平台数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid PlatformUpdateDTO dto) {
        Optional.ofNullable(platformManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        PlatformEntity entity = platformConvert.update(dto);
        return platformManager.updateById(entity);
    }

    /**
     * 根据id删除 系统平台数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return platformManager.removeById(id);
    }

    /**
     * 根据id删除 系统平台数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(PlatformDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
